*
* Purpose:
*	Identify dominated choices
*

use "${raw_data_applications_${year}}", clear
	keep if eljaras == 1
	merge n:1 karkod szaknev szint munkarend fin_form using "${data_contracts_${year}}"
	drop if _m == 2
	drop _m

	label define state_funded 0 "Self-funded" 1 "State-funded"
	do "${helpers_dir}/tidyVariableForWelfareAnalysis.do"
	
	merge 1:1 id rank using "${data_applications_ps_${year}}"
	drop if _m == 2
	drop _m
	
	* Admitted, priority score is below priority-score cutoff
	do "${helpers_dir}/addAdmittedBelowPSCutoff.do"

	* Rejected, priority score is above priority-score cutoff
	do "${helpers_dir}/addHasJustifiedEnvy.do"
	
	drop priority_score_cutoff 
	drop fin_form
	gen fin_form=1
	merge n:1 karkod szaknev szint munkarend fin_form using "${data_contracts_${year}}", keepusing(ponthatar)
	drop if _m == 2
	gen no_funded_version = (_merge == 1) & state_funded == 0
	drop _merge
	rename ponthatar priority_score_cutoff	
	lab var priority_score_cutoff "Priority score cutoff (state-funded)"	

	merge n:1 karkod szaknev szint munkarend fin_form using "${correct_ps_cutoffs_${year}}", keepusing(ps_cutoff_funded_corrected) 
	drop _m
	
	
	* 1 Identify ROL type ------------------------------------------------------
	bys applicant_id: egen state_funded_min = min(state_funded)
	bys applicant_id: egen state_funded_max = max(state_funded)

	gen ROL_type = .
	replace ROL_type = 0 if state_funded_min == 1 & state_funded_max == 1
	replace ROL_type = 1 if state_funded_min == 0 & state_funded_max == 1
	replace ROL_type = 2 if state_funded_min == 0 & state_funded_max == 0
	drop state_funded_min state_funded_max

	label define ROL_type 0 "Only state-funded contracts" 1 "State- and self-funded contracts" 2 "Only self-funded contracts"
	label values ROL_type ROL_type
	label variable ROL_type "ROL type"

	gen ROL_state_funded_only = ROL_type == 0
	lab var ROL_state_funded_only "\ - only state-funded contracts"
	gen ROL_state_self_funded = ROL_type == 1
	lab var ROL_state_self_funded "\ - state- and self-funded contracts"
	gen ROL_self_funded_only = ROL_type == 2
	lab var ROL_self_funded_only "\ - only self-funded contracts"
	
	bys applicant_id: egen state_funded_max_rank = max((state_funded == 1) * rank)
	bys applicant_id: egen self_funded_min_rank = max((state_funded == 0) * rank)
	gen all_state_f_above_self_f = state_funded_max_rank < self_funded_min_rank
	replace all_state_f_above_self_f = . if ROL_type == 0 | ROL_type == 2
	lab var all_state_f_above_self_f "All state-funded above self-funded"
	drop state_funded_max_rank self_funded_min_rank
	
	gen contract_num = osszjel
	lab var contract_num "Number of contracts on ROL"
	
	rename felv_sorsz1 admission_rank
	lab var admission_rank "Rank of admission"
	
	gen ba_program = szint == 1 | szint == 3
	bys applicant_id: egen any_ba_listed = max(ba_program)
	lab var any_ba_listed "Ranked a BA program"
	
	bys applicant_id: egen no_funded_version_at_all = min(no_funded_version)
	
	gen fulltime = munkarend == 1
	bys applicant_id: egen fulltime_max = max(fulltime)
	
	* 2 Contracts/programs per applicant ------------------------------------------------
	bys applicant_id: gen contract_num_data = _N
	label var contract_num_data "Number of contracts on ROL (data)"
	
	bys applicant_id program_id: gen counter = _n
	bys applicant_id: egen program_num_data = sum(counter == 1)
	drop counter
	lab var program_num_data "Number of programs on ROL (data)"
	
	
	* 3 Identify dominated choices ---------------------------------------------
	** 3.1 Dominated flipping --------------------------------------------------
	duplicates tag applicant_id program_id, gen(both_programs_ranked)

	* Rank applications within university program
	sort applicant_id program_id rank
	by applicant_id program_id: gen rank_within_program = _n
	replace rank_within_program = . if both_programs_ranked == 0

	gen dominated_flipping_temp = (state_funded == 0 & rank_within_program == 1)

	bys applicant_id program_id: egen dominated_flipping_program = max(dominated_flipping_temp)
	replace dominated_flipping_program = 0 if szint == 2 | szint == 4 | szint == 5 | szint == 6
	label var dominated_flipping_program "Dominated flipping (program-level)"

	bys applicant_id: egen dominated_flipping_applicant_id = max(dominated_flipping_program)
	label var dominated_flipping_applicant_id "Dominated flipping (applicant-level)"

	cap drop dominated_flipping_temp rank_within_program

	** 4.2 Dominated dropping --------------------------------------------------
	** Step 1: List only unfunded position
	gen dominated_dropping_step1_temp = (both_programs_ranked == 0 & state_funded == 0)

	** Step 2: No funded counterpart
	gen dominated_dropping_step2_temp = dominated_dropping_step1_temp
	replace dominated_dropping_step2_temp = 0 if no_funded_version == 1

	* Step 3: No mistake if rank is larger than 6
	replace dominated_dropping_step2_temp = 0 if rank > 6
	
	replace dominated_dropping_step2_temp = 0  if szint == 2 | szint == 4 | szint == 5 | szint == 6

	bys applicant_id program_id: egen dominated_dropping_program = max(dominated_dropping_step2_temp)
	lab var dominated_dropping_program "Dominated dropping (program-level)"

	bys applicant_id: egen dominated_dropping_applicant_id = max(dominated_dropping_step2_temp)
	lab var dominated_dropping_applicant_id "Dominated dropping (applicant-level)"

	cap drop *_temp both_programs_ranked fin_form



preserve

	* Save dominated dropping
	keep if dominated_dropping_program == 1
	keep if admitted_below_ps_cutoff == 0 & has_justified_envy == 0
	keep if priority_score > 0
	replace state_funded = 1
	keep id applicant_id rank program_id state_funded priority_score
	
	merge n:1 id applicant_id using "${data_applicants_${year}.dta}", keepusing(sample_hss eligibility)
	keep if _merge  == 3
	drop _m

	save "${data_drop_${year}}", replace


restore


preserve 
	* Save dominated flipping
	keep if dominated_flipping_program == 1
	
	bys applicant_id program_id: egen rank_min = min(rank)
	keep if state_funded == 1
	drop rank
	rename rank_min rank
	keep if admitted_below_ps_cutoff == 0 & has_justified_envy == 0
	keep if priority_score > 0
	keep id applicant_id rank program_id state_funded priority_score
	
	merge n:1 id applicant_id using "${data_applicants_${year}.dta}", keepusing(sample_hss eligibility)
	keep if _merge  == 3
	drop _m

	save "${data_flip_${year}}", replace
restore


merge 1:1 id rank using "${data_applications_ps_${year}}"
drop _m

	
	* ------------------------------------------------------------------------------
	* Block 3
	* Costly dominated choices: dominated flipping
	* Costly dominated flipping - would have been admitted to the funded version
	gen costly_dominated_flip_prog = (priority_score >= ps_cutoff_funded_corrected) if state_funded == 1 & dominated_flipping_program == 1
	bys applicant_id: egen costly_dominated_flip_id = max(costly_dominated_flip_prog) 
	bys applicant_id program_id: egen costly_dominated_flip_prog_temp = max(costly_dominated_flip_prog)

	* Costly dominated flipping: lower bound
	gen costly_dominated_flip_rank = rank if costly_dominated_flip_prog_temp == 1
	by applicant_id: egen costly_dominated_flip_rank_min = min(costly_dominated_flip_rank)
	gen costly_dominated_flip_DA_id = (costly_dominated_flip_rank_min <= admission_rank)
	replace costly_dominated_flip_DA_id = . if dominated_flipping_applicant_id == 0

	gen costly_dominated_flip_DA_prog = costly_dominated_flip_prog
	replace costly_dominated_flip_DA_prog = 0 if costly_dominated_flip_DA_id == 0 & costly_dominated_flip_prog == 1

	drop costly_dominated_flip_prog_temp costly_dominated_flip_rank costly_dominated_flip_rank_min
	
	* ------------------------------------------------------------------------------
	* Block 4
	* Costly mistakes: obvious dropping
	*** Binding mistake - based on this year's admission cutoff and admission score
	gen costly_dominated_drop_prog = .
	replace costly_dominated_drop_prog = (priority_score >= ps_cutoff_funded_corrected) if ps_cutoff_funded_corrected != . & state_funded == 0
	replace costly_dominated_drop_prog = 0 if priority_score_cutoff== . & state_funded == 0
	replace costly_dominated_drop_prog = . if dominated_dropping_program == 0

	* Non-binding if admitted to a higher ranked program (and the cutoff is non-missing)
	gen costly_dominated_drop_DA_prog = costly_dominated_drop_prog
	replace costly_dominated_drop_DA_prog = 0 if admission_rank < rank & admission_rank != . & costly_dominated_drop_prog != . & dominated_dropping_program == 1

	bys applicant_id: egen costly_dominated_drop_id = max(costly_dominated_drop_prog)
	bys applicant_id: egen costly_dominated_drop_DA_id = max(costly_dominated_drop_DA_prog)

	replace costly_dominated_drop_id = . if dominated_dropping_applicant_id == 0
	replace costly_dominated_drop_DA_id = . if dominated_dropping_applicant_id == 0
		
	* 5 Label variables --------------------------------------------------------
	
	lab var dominated_dropping_applicant_id "Dominated dropping (applicant)"
	lab var dominated_dropping_prog "Dominated dropping (program)"
	lab var dominated_flipping_applicant_id "Dominated flipping (applicant)"
	lab var dominated_flipping_prog "Dominated flipping (program)"
	lab var costly_dominated_flip_id "Costly dominated flipping (lower)"
	lab var costly_dominated_drop_id "Costly dominated dropping (lower)"
	lab var costly_dominated_flip_prog "Costly dominated flipping (lower), program"
	lab var costly_dominated_drop_prog "Costly dominated dropping (lower), program"
	lab var costly_dominated_flip_DA_id "Costly dominated flipping (upper)"
	lab var costly_dominated_drop_DA_id "Costly dominated dropping (upper)"
	lab var costly_dominated_flip_DA_prog "Costly dominated flipping (upper), program"
	lab var costly_dominated_drop_DA_prog "Costly dominated dropping (upper), program"
	
	* Add covariates
	merge n:1 id applicant_id using "${data_applicants_${year}.dta}", ///
		keepusing(female tazon NUTSregion location_size ///
			nationality schoolid exam_year eligibility GPA11 GPA11_std ///
			GPA11_missing year age sec_school voc_school ///
			other_school capital countycapital town village disadv ///
			sample_hss contract_num)
	drop _m
	
	* Save data ----------------------------------------------------------------
	keep applicant_id id rank program_id contract_id state_funded admitted admission_rank ///
		year ///
		ROL_type contract_num ///
		dominated* costly* ///
		priority_score  priority_score_cutoff ///
		field ///
		female tazon NUTSregion location_size ///
		nationality schoolid exam_year eligibility GPA11 ///
		GPA11_missing GPA11_std year age sec_school voc_school ///
		other_school capital countycapital town village disadv ///
		sample_hss contract_num ROL_* *_data ///
		any_ba_listed ba_program no_funded_version_at_all all_state_f_above_self_f ///
		szint munkarend Stem field_study karkod szekhely kepztelep fulltime_max szaknev 
	
	compress		
	save "${data_applications_tidy_${year}}", replace

	* Collapse data to individual-level ----------------------------------------
	keep if rank == 1

	cap drop admitted contract_id *_prog *_program field_study ///
		priority_score priority_score_cutoff program_id rank ///
		szint munkarend Stem field_study karkod szekhely kepztelep szaknev 
	
	compress	
	save "${data_applicants_tidy_${year}}", replace
